package com.yx.changdao.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yx.changdao.common.annotation.AutoLog;
import com.yx.changdao.common.annotation.ShiroDesc;
import com.yx.changdao.common.data.Pager;
import com.yx.changdao.common.data.Result;
import com.yx.changdao.common.entity.Problem;
import com.yx.changdao.common.entity.SysUser;
import com.yx.changdao.common.exception.AlertException;
import com.yx.changdao.common.utils.DesensitizationUtils;
import com.yx.changdao.common.utils.Faster;
import com.yx.changdao.common.utils.SysConst;
import com.yx.changdao.common.utils.SysUtils;
import com.yx.changdao.common.vo.ProblemVo;
import com.yx.changdao.service.ProblemService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 问题处理模块
 * @auther xiapeixin
 * @date 2020-04-07
 */
@RestController
@RequestMapping("/problem/process")
public class ProblemProcessController {
    /**
     * 问题服务
     */
    @Autowired
    private ProblemService problemService;

    /**
     * 列表
     *
     * @return Result
     */
    @AutoLog
    @GetMapping("/list")
    @RequiresPermissions("problem:process:list")
    @ShiroDesc("问题办理:列表")
    public Result list(@RequestParam(value = "page") Long page,
                       @RequestParam(value = "limit", required = false) Integer limit) {

        if (limit == null) {
            limit = SysConst.LIMIT_PAGE;
        }

        // 分页配置
        Page<Problem> pager = new Page<>(page, limit);
        // 根据请求解析条件。
        QueryWrapper<Problem> qw = SysUtils.autoCnd(Problem.class);

        qw.orderByDesc(Problem.CREATE_TIME); //时间倒序

        SysUser sysUser = SysUtils.currentUser();

        qw.eq(Problem.DEP_ID, sysUser.getDepId()); //自己部门处理的;
        qw.eq(Problem.DIFFICULT_LEVEL_STATUS,2); //办理难度通过的


        if("2".equals(SysUtils.getRequest().getParameter("eq_ifTimeout"))){
            qw.eq(Problem.IF_END,0);
            qw.in(Problem.IF_DEAL, 0, 2);
        }


        // 获取分页结果集。
        IPage<ProblemVo> voIPage = this.problemService.pageNex(pager, qw);

        voIPage.getRecords().forEach(t ->{
            // 身份证脱敏
            t.setResidentIdcard(DesensitizationUtils.idEncrypt(t.getResidentIdcard()));
            // 剩余天数
            t.setLastDay(problemService.lastDay(Faster.now(),t.getDealLimitTime()));

            //断是否可用提交处理结果
            t.setEnableDeal(true); //可提交
            if(t.getIfDeal().equals(1)){
                t.setEnableDeal(false);  //已处理不可提交
            }

        });


        Map<String, Object> map = new HashMap<>();
        map.put("list", voIPage.getRecords());
        map.put("pager", new Pager(voIPage));

        Map<String, Object> statusCount = this.statusCount(sysUser.getDepId());
        map.put("statusCount", statusCount);
        return Result.ok().data(map);
    }

    // 统计各个状态下的问题总数
    private Map<String, Object> statusCount(Integer depId) {
        QueryWrapper<Problem> qw = new QueryWrapper<>();
        qw.eq(Problem.DEP_ID, depId); //自己部门处理的;
        qw.eq(Problem.DIFFICULT_LEVEL_STATUS,2); //办理难度通过的

        // 未处理 0
        QueryWrapper<Problem> qw1 = qw.clone();
        qw1.eq(Problem.IF_DEAL, 0);
        int notDeal = problemService.count(qw1);
        // 已处理 1
        QueryWrapper<Problem> qw2 = qw.clone();
        qw2.eq(Problem.IF_DEAL, 1);
        int dealed = problemService.count(qw2);
        // 打回处理 2
        QueryWrapper<Problem> qw3 = qw.clone();
        qw3.eq(Problem.IF_DEAL, 2);
        int back2Deal = problemService.count(qw3);
        // 已逾期 1
        QueryWrapper<Problem> qw4 = qw.clone();
        qw4.eq(Problem.IF_TIMEOUT, 1);
        int timeout = problemService.count(qw4);

        // 即将逾期 2
        QueryWrapper<Problem> qw5 = qw.clone();
        qw5.eq(Problem.IF_TIMEOUT, 2);

        qw5.eq(Problem.IF_END,0);
        qw5.in(Problem.IF_DEAL, 0, 2);
        int soonTimeOut = problemService.count(qw5);

        // 疑难件 1
        QueryWrapper<Problem> qw6 = qw.clone();
        qw6.eq(Problem.IF_DIFFICULT, 1);
        int difficult = problemService.count(qw6);

        Map<String, Object> map = new HashMap<>();
        map.put("notDeal", notDeal);
        map.put("dealed", dealed);
        map.put("back2Deal", back2Deal);
        map.put("timeout", timeout);
        map.put("soonTimeOut", soonTimeOut);
        map.put("difficult", difficult);

        return map;
    }



    /**
     * 疑难件申请
     *
     * @param problemId 问题id
     */
    @AutoLog
    @PutMapping("/apply")
    /*@RequiresPermissions("problem:process:apply")
    @ShiroDesc("问题办理:提交申请")*/
    public Result reject(@RequestParam("problemId") Integer problemId,
                         @RequestParam(value = "applyDesc",required = false)String applyDesc) throws AlertException {

        if (!problemService.apply(problemId,applyDesc)) {
            throw new AlertException("提交申请失败");
        }
        return Result.ok("申请成功");
    }

    /**
     * 问题处理提交结果
     *
     */
    @AutoLog
    @PutMapping("/submit")
    /*@RequiresPermissions("problem:process:submit")
    @ShiroDesc("问题办理:提交结果")*/
    public Result reject(@RequestParam("problemId") Integer problemId,// 问题id
                         @RequestParam("name") String name,//问题处理人名
                         @RequestParam("content") String content,// 问题处理结果
                         @RequestParam("dealPhone") String dealPhone,// 问题处理人手机号
                         @RequestParam(value = "imgs", required = false) String imgs) throws AlertException {

        boolean b = problemService.submit(problemId, name, dealPhone, content, imgs);
        if (!b) {
            throw new AlertException("提交处理结果失败");
        }
        return Result.ok("提交处理结果成功");
    }

}
